<?php
/**
 * Created by PhpStorm.
 * User: Keter
 * Date: 2016/8/3
 * Time: 16:55
 */
class RegisterLogQueen extends StatsLogBase
{
    public $queenName = 'registerLog';
    public $baseTableName = 'user_info';
    public $dbName = 'user_info';

    /**
     * 推入队列
     * @param $data
     * @return bool
     * @throws Exception
     */
    public function push($data)
    {
        return parent::pushQueen($data,'push_register_log_error', 'user_stats');
    }

    /**
     * 保存到数据库
     * @param $data
     * @return array
     */
    public function saveLog($data)
    {
        /**
         * 检测分库
         */
        $result = $this->checkDatabase($data['registerTime']);
        if (!$result) {
            $this->e('cannot_select_database');
            return false;
        }

        /**
         * 检查表是否存在
         */
        if (!$this->checkTableExists()) {
            return [];
        }

        /**
         * 检查用户是否存在
         */
        $exists = $this->mysql->findRow($this->tableName, ['uid' => $data['uid']]);
        if ($exists) {
            $this->e('用户已存在', $data);
            return [];
        }

        /**
         * 保存数据
         */
        $data = [
            'uid'          => $data['uid'],
            'username'     => $data['username'],
            'sex'          => $data['sex'],
            'nickname'     => $data['nickname'],
            'avatar'       => $data['avatar'],
            'address'      => $data['address'],
            'password'     => $data['password'],
            'salt'         => $data['salt'],
            'token'        => $data['token'],
            'status'       => $data['status'],
            'platform'     => $data['platform'],
            'channelID'    => $data['channelID'],
            'ver'          => $data['ver'],
            'imei'         => $data['imei'],
            'imsi'         => $data['imsi'],
            'mac'          => $data['mac'],
            'model'        => $data['model'],
            'freeDiamond'  => $data['freeDiamond'],
            'paidDiamond'  => $data['paidDiamond'],
            'registerIP'   => $data['registerIP'],
            'registerTime' => $data['registerTime'],
            'createTime'   => time()
        ];

        $result = $this->mysql->insert($this->tableName, $data);
        if (!$result) {
            $this->e('save_error', $this->mysql->error());
            $this->e('新用户日志保存失败', $data);
            return [];
        }

        return $data;
    }

    /**
     *
     * @param $tableName
     * @return string
     */
    public function getCreateTableSql($tableName)
    {
        $sql = "CREATE TABLE `{$tableName}` (
              `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
              `uid` int(11) unsigned NOT NULL,
              `username` varchar(32) NOT NULL,
              `sex` tinyint(1) NOT NULL DEFAULT 0,
              `nickname` varchar(255) NOT NULL DEFAULT '',
              `avatar` varchar(255) NOT NULL DEFAULT '',
              `address` varchar(255) NOT NULL DEFAULT '',
              `password` varchar(30) NOT NULL DEFAULT '',
              `salt` char(8) NOT NULL DEFAULT '',
              `token` varchar(50) NOT NULL DEFAULT '',
              `status` tinyint(1) NOT NULL DEFAULT 0,
              `platform` tinyint(1) NOT NULL DEFAULT 0,
              `channelID` int(11) unsigned NOT NULL DEFAULT 0,
              `ver` varchar(30) NOT NULL DEFAULT '',
              `imei` varchar(100) NOT NULL DEFAULT '',
              `imsi` varchar(100) NOT NULL DEFAULT '',
              `mac` varchar(100) NOT NULL DEFAULT '',
              `model` varchar(20) NOT NULL DEFAULT '',
              `freeDiamond` bigint(20) NOT NULL DEFAULT 0,
              `paidDiamond` bigint(20) NOT NULL DEFAULT 0,
              `registerIP` varchar(32) NOT NULL DEFAULT '',
              `registerTime` int(11) NOT NULL DEFAULT 0,
              `createTime` int(11) NOT NULL DEFAULT 0,
              PRIMARY KEY (`id`),
              UNIQUE KEY `uid` (`uid`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户表'";

        return $sql;
    }
}